package org.rcy.framework.thrift.serializer;

import net.qiyuesuo.rpc.thrift.metadata.FieldValueMetaData;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;

import java.util.Date;

/**
 * 日期类型序列化(转为long型处理)
 * @author zhouree
 *
 */
public class DateSerializer implements Serializer{

	@Override
	public Object read(TProtocol iprot, FieldValueMetaData valueMetaData) throws TException {
		long value = iprot.readI64();
		return new Date(value);
	}

	@Override
	public void write(TProtocol oprot, FieldValueMetaData valueMetaData, Object value) throws TException {
		if (value == null) {
			return;
		}
		if (value instanceof Date) {
			Date date = (Date) value;
			oprot.writeI64(date.getTime());
		}
	}

}
